import axios from 'axios';
import { apiServer } from "@/utils/config";
import Vue from 'vue';
import { Toast } from 'vant';
// import Router from '@/router/index'
import Store from '@/vuex/store.js'
Vue.use(Toast);
// 通用公用方法
// https://zsy-api.tsyxvip.com/api/goods/home?channelNo=ZC210406KgNL17KH&tempType=t2
const req = (method, url, params) => {

    var _self = this
    return axios({
        method: method,
        url: apiServer + url,
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            token: localStorage.getItem('logintoken'),
            corpCode: 'QY'
        },
        data: params,
        traditional: true,
        transformRequest: [
            function (data) {
                let ret = ''
                for (let it in data) {
                    ret +=
                        encodeURIComponent(it) +
                        '=' +
                        encodeURIComponent(data[it]) +
                        '&'
                }
                return ret
            }
        ]
    }).then(res => {
        // console.log(res.data)
        const code = res.data.rspCode
        if (code == '200004') {
            Toast('用户登录超时，请重新登录！');
            window.localStorage.setItem("userMsg", JSON.stringify({}));
            window.localStorage.setItem('logintoken', '')
            window.localStorage.setItem('phone', '')
            Store.state.isright = false;
        }
        return res.data
    });
};

export default req